package com.wjc.myktapplication.page.wxaccount

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import com.wjc.basemodule.utils.LogTools
import com.wjc.common.launch
import com.wjc.myktapplication.data.NetworkManager
import com.wjc.myktapplication.data.wxaccount.WxAccountBean
import com.wjc.network.ResponseResult
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

/**
 * ClassName: com.wjc.myktapplication.page.wxaccount
 * Description:
 * JcChen on 2021.04.18.15:01
 */
class AccountViewModel : ViewModel() {
    private val mAccountReq = AccountReq()
    var wxAccountList = MutableLiveData<ResponseResult<MutableList<WxAccountBean>>>()
    var result: ResponseResult<MutableList<WxAccountBean>>? = null
    var wxAccountListF = MutableStateFlow(result)

    fun fetchWxAccountList() {
        mAccountReq.wxAccountList(wxAccountList)
    }

    // tag 列表
    fun fetchWxAccountListS() {
        launch({
            val result = async {
                NetworkManager.fetchApiService().fetchWxArticleListS()
            }
            LogTools.d("TAG", ":result=$result ");
            wxAccountList.value = result.await()
        })
    }

    fun fetchWxAccountListF() {
        flow { NetworkManager.fetchApiService().fetchWxArticleListF().let { emit(it) } }
    }

    /**
     * 获取具体公众号的文字列表
     */
    fun fetWxArtLists(authId: Int) = Pager(PagingConfig(pageSize = 10)) {
        LogTools.d("TAG", ":authId =$authId ")
        WxArtSource(authId)
    }.flow


}